package com.ttx;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.*;
/**
 * Created by ttx on 17-9-20.
 */
public class URLExtractor implements Runnable{
    private List<String> articleStrings;
    private List<List<String>> urls;
    private int urlNum = 0;

    URLExtractor(Properties prop, List<List<String>> urls, List<String> articleStrings){
        this.urls = urls;
        this.articleStrings = articleStrings;
    }

    public void readArticleData() {
           for(String singleArticle:articleStrings){
                String[] s = singleArticle.split("\t");
                if (s.length == 7) {
                    List<String> urlList = extractImgOriginalURL(s[5]);
                    urlNum += urlList.size();
                    urls.add(urlList);
                } else {
                    System.out.println("错误：数据格式异常！数据：" + singleArticle);
                }
            }
    }

    //先从网页抽取相关信息，然后进行筛选，获得boolean 数组, 根据该数据设置image，然后返回有效imageid


    //如果不存在，则表示为空
    List<String> extractImgOriginalURL(String contentStr) {
        List<String> imgOriginalURLList = new ArrayList<String>();
        Document article = Jsoup.parse(contentStr);
        Elements img = article.select("img");

        //统计带图文章数 & 图片数(可能重复)

        for (Element s : img) {
            //统计带src属性img数量
            if (s.hasAttr("src")) {
                String t = s.attr("src");
                if (t.charAt(0) == '\\') {      //处理元数据中imgURL的转义引号字符
                    if (t.split("\\\\\"").length == 0) {
                        imgOriginalURLList.add("");
                    } else {
                        String imgOriginalURL = t.split("\\\\\"")[1];
                        imgOriginalURLList.add(imgOriginalURL);
                    }
                } else {
                    String imgOriginalURL = t;
                    if (imgOriginalURL == null || imgOriginalURL == "") {
                        imgOriginalURLList.add("");
                    } else {
                        imgOriginalURLList.add( imgOriginalURL );     //计算保存路径
                    }

                }
            } else {
                imgOriginalURLList.add("");
            }
        }
        return imgOriginalURLList;
    }

    @Override
    public void run(){
        readArticleData();
        System.out.println("thread:" + urlNum);
    }

}
